package org.example.musicserver.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.musicserver.Request.UserRequest;
import org.example.musicserver.common.Result;
import org.example.musicserver.entity.User;
import org.example.musicserver.mapper.UserMapper;
import org.example.musicserver.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public Result loginCheck(String name, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        queryWrapper.eq("password", password);
        User user = userMapper.selectOne(queryWrapper); // 执行查询
        if (user != null) {
            // 找到了匹配的用户
            return Result.success("登陆成功", user); // 假设Result.success静态方法用于封装成功响应，具体取决于Result类的实现
        } else {
            // 没有找到匹配的用户
            return Result.error("用户不存在或密码错误"); // 假设Result.fail静态方法用于封装失败响应，具体取决于Result类的实现
        }
    }

    @Override
    public Result getAllUser(String query) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (query != null) {

            queryWrapper.like("name", query);
        }
        return Result.success(null, userMapper.selectList(queryWrapper));
    }

    @Override
    public Result updateUserMsg(UserRequest updateRequest) {
        User user = new User();
        BeanUtils.copyProperties(updateRequest, user);
        if (userMapper.updateById(user) > 0) {
            return Result.success("修改成功");
        } else {
            return Result.error("修改失败");
        }
    }

    //根据用户id获取某个用户信息
    @Override
    public Result getUserMsg(Integer id) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        return Result.success("获取用户信息成功", userMapper.selectOne(queryWrapper));
    }

    //根据id删除某个用户
    @Override
    public Result deleteUser(Integer id) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        if (userMapper.delete(queryWrapper) > 0) {
            return Result.success("删除用户成功", true);
        } else {
            return Result.error("删除用户失败");
        }
    }

    //根据列表删除多个用户
    @Override
    public Result deleteUsers(@RequestParam("userIds[]") List<Integer> userIds) {
        int deletedCount = userMapper.deleteBatchIds(userIds);
        //QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (deletedCount > 0) {
            return Result.success("删除用户成功", true);
        } else {
            return Result.error(deletedCount + "删除用户失败");
        }
    }

    //这里没有考虑同名的情况
    @Override
    public Result addUser(UserRequest registryRequest) {

        User user = new User();
        BeanUtils.copyProperties(registryRequest, user);
        //MD5加密，到时候再使用
        //String password = DigestUtils.md5DigestAsHex((SALT + registryRequest.getPassword()).getBytes(StandardCharsets.UTF_8));
        //consumer.setPassword(password);
        //都用用
//        if (StringUtils.isBlank(consumer.getPhoneNum())) {
//            consumer.setPhoneNum(null);
//        }
//        if ("".equals(consumer.getEmail())) {
//            consumer.setEmail(null);
//        }
        user.setAvatar("img/avatorImages/user.jpg");
        try {
            if (userMapper.insert(user) > 0) {
                return Result.success("注册成功");
            } else {
                return Result.error("注册失败");
            }
        } catch (DuplicateKeyException e) {
            return Result.fatal(e.getMessage());
        }
    }

    @Override
    public Result registerService(String name, String password) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        List<User> users = userMapper.selectList(queryWrapper);

        if (!users.isEmpty()) {
            return Result.error("该用户名已被注册");
        }

        User user = new User();
        user.setName(name);
        user.setPassword(password);
        user.setSex((byte) 2);
        user.setAvatar("/img/avatorImages/user.jpg");
        userMapper.insert(user);

        return Result.success("注册成功", userMapper.selectOne(queryWrapper));

    }

}
